#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > walk_polar2.info <<'%EOF%'
   walk_polar2
   -----------
   Molecule:         O3
   Wave Function:    MCSCF (CAS) / aug-cc-pVDZ
   Test Purpose:     Geometry optimization using .WALK module (2nd order),
                     with a calculation of static and dynamic
                     polarizabilities using all three different ways of
                     calculating them, vibrational analysis and Cioslowski
                     population anaylsis
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > walk_polar2.mol <<'%EOF%'
BASIS
aug-cc-pVDZ
Geometry optimization using WALK module plus
static and dynamic polarizabilities
Atomtypes=1 Generators=2   Y  Z
Charge=8.0 Atoms=2
OS         .0000000000        0.0000000000         .0000000000             *
OP        1.2634502500        2.0449019100         .0000000000             *
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > walk_polar2.dal <<'%EOF%'
**DALTON INPUT
.WALK
**WAVE FUNCTIONS
.HF
.MP2
.MCSCF
.NSYM
 4
*SCF INPUT
.DOUBLY OCCUPIED
 6 4 1 1
*CONFIGURATION INPUT
.SYMMETRY
 1
.SPIN MUL
 1
.INACTIVE
 4 2 0 0
.ELECTRONS
 12
.CAS SPACE
 3 3 2 1
*OPTIMIZATION
.DETERMINANTS
**START
.PRINT
 4
*TWOEXP   ! checking PTRAN and PSORT
.PTRPRI
 9
.SORPRI
 9
**PROPERTIES
.POLARI
.VIBANA
.POPANA
.ALPHA
*ABALNR
.FREQUE
 3
 0.0 0.0592 0.096
**RESPONSE
*LINEAR
.DIPLEN
.FREQUE
 2
0.0 0.0592
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >walk_polar2.check
cat >>walk_polar2.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# Reading molecular geometry:
CRIT1=`$GREP "1 * x * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "4 * x * 1\.2634502500" $log | wc -l`
CRIT3=`$GREP "5 * y * 2\.0449019100" $log | wc -l`
CRIT4=`$GREP "7 * x * 1\.2634502500" $log | wc -l`
CRIT5=`$GREP "8 * y * \-2\.0449019100" $log | wc -l`
TEST[1]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5`
CTRL[1]=5
ERROR[1]="MOLECULAR GEOMETRY NOT CORRECT"

# Nuclear repulsion energy:
CRIT1=`$GREP "Nuclear repulsion energy : * 68\.8991880907[0-9][0-9]" $log | wc -l`
TEST[2]=`expr $CRIT1`
CTRL[2]=1
ERROR[2]="NUCLEAR REPULSION ENERGY NOT CORRECT"

# Symmetry:
CRIT1=`$GREP "Number of coordinates in each symmetry: * 3 * 3 * 2 * 1" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry: * 27 * 21 * 12 * 9" $log | wc -l`
TEST[3]=`expr $CRIT1  \+ $CRIT2`
CTRL[3]=2
ERROR[3]="SYMMETRY IS NOT CORRECT"

# SCF energy, 1st iteration:
CRIT1=`$GREP "Hartree\-Fock total * energy\: * \-224\.2905002437" $log | wc -l`
TEST[4]=`expr $CRIT1`
CTRL[4]=1
ERROR[4]="INITIAL HF ENERGY NOT CORRECT"

# MCSCF energy, 1st iteration:
CRIT1=`$GREP "MP2 second order energy *\: * \-224\.963913" $log | wc -l`
CRIT2=`$GREP "Final MCSCF energy\: * \-224\.5185239" $log | wc -l`
TEST[5]=`expr $CRIT1  \+ $CRIT2`
CTRL[5]=2
ERROR[5]="INITIAL MCSCF ENERGY IS NOT CORRECT"

# Occupancies, 1st iteration:
#CRIT1=`$GREP "2.000000000 * 2.000000000 * 2.000000000 * 2.000000000 * 1.99018181[0-9]" $log | wc -l`
#CRIT2=`$GREP "1.9572361.[0-9] * (0| ).0602612.[0-9]" $log | wc -l`
#CRIT3=`$GREP "2.000000000 * 2.000000000 * 1.9919875.[0-9] * 1.9487837.[0-9] * (0| ).05066022[0-9]" $log | wc -l`
#CRIT4=`$GREP "1.9636420.[0-9] * (0| ).2484928.[0-9]" $log | wc -l`
#CRIT5=`$GREP "1.7887544.[0-9]" $log | wc -l`
#TEST[6]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5`
#CTRL[6]=15
TEST[6]=0
CTRL[6]=0
ERROR[6]="INITIAL OCCUPANCIES NOT CORRECT"

# SCF energy, 1st iteration:
CRIT1=`$GREP "Total energy * \-224\.518523910[0-9] au \(Hartrees\)" $log | wc -l`
CRIT2=`$GREP "\-6109\.4598163[0-9] eV" $log | wc -l`
CRIT3=`$GREP "\-589473\.293[0-9] kJ\/mol" $log | wc -l`
TEST[7]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[7]=3
ERROR[7]="INITIAL ENERGY NOT CORRECT"

# Gradient, 1st iteration:
CRIT1=`$GREP "OS * x * (0| )\.0099" $log | wc -l`
CRIT2=`$GREP "OP * x * (\-0|\-)\.0099" $log | wc -l`
CRIT3=`$GREP "OP * y * (\-0|\-)\.0189" $log | wc -l`
TEST[8]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[8]=7
ERROR[8]="INITIAL GRADIENT NOT CORRECT"

# Hessian, 1st iteration:
CRIT1=`$GREP "OS * x * (0| )\.4296" $log | wc -l`
CRIT2=`$GREP "OP * x * (\-0|\-)\.4296.[0-9] * (0| )\.4296" $log | wc -l`
CRIT3=`$GREP "OP * y * (\-0|\-)\.2514.[0-9] * (0| )\.2514.[0-9] * (0| )\.9240.[0-9]" $log | wc -l`
CRIT4=`$GREP "OS * y * (0| )\.4303.[0-9]" $log | wc -l`
CRIT5=`$GREP "OP * x * (\-0|\-)\.2707.[0-9] * (0| )\.1579.[0-9]" $log | wc -l`
CRIT6=`$GREP "OP * y * (\-0|\-)\.4303.[0-9] * (0| )\.2707.[0-9] * (0| )\.4303.[0-9]" $log | wc -l`
CRIT7=`$GREP "OS * z * (\-0|\-)\.00784[0-9]" $log | wc -l`
CRIT8=`$GREP "OP * z * (0| )\.00784[0-9] * (\-0|\-)\.00784[0-9]" $log | wc -l`
CRIT9=`$GREP "OP * z * (\-0|\-)\.00927[0-9]" $log | wc -l`
TEST[9]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9`
CTRL[9]=10
ERROR[9]="INITIAL HESSIAN NOT CORRECT"

# Dipole moment, 1st iteration:
CRIT1=`$GREP "(0| )\.216(5|6).[0-9] * (0| )\.5505.[0-9] * 1\.836..[0-9]" $log | wc -l`
CRIT2=`$GREP "x * (\-0|\-)\.216....[0-9] * (\-0|\-)\.5505...[0-9] * \-1.836....[0-9]" $log | wc -l`
TEST[10]=`expr $CRIT1  \+ $CRIT2`
CTRL[10]=3
ERROR[10]="INITIAL DIPOLE MOMENT NOT CORRECT"

# Step, 1st iteration:
CRIT1=`$GREP "OP *_1 * (0| )\.00438....[0-9] * (0| )\.0169.....[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "OP *_2 * (0| )\.00438....[0-9] * (\-0|\-)\.0169.....[0-9] * (0| )\.0000000000" $log | wc -l`
TEST[11]=`expr $CRIT1  \+ $CRIT2`
CTRL[11]=4
ERROR[11]="INITIAL STEP NOT CORRECT"

# Geometry, 2nd iteration:
CRIT1=`$GREP "OP *_1 * 1\.2678[0-9]* * 2\.0618[0-9]* * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "OP *_2 * 1\.2678[0-9]* * \-2\.0618[0-9]* * (0| )\.0000000000" $log | wc -l`
TEST[12]=`expr $CRIT1  \+ $CRIT2`
CTRL[12]=4
ERROR[12]="SECOND GEOMETRY NOT CORRECT"

# Energy, 2nd iteration:
CRIT1=`$GREP "Total energy * \-224\.5187564[0-9]* au \(Hartrees\)" $log | wc -l`
CRIT2=`$GREP "\-6109\.466144[0-9]* eV" $log | wc -l`
CRIT3=`$GREP "\-589473\.904[0-9] kJ\/mol" $log | wc -l`
TEST[13]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[13]=3
ERROR[13]="SECOND ENERGY NOT CORRECT"

# Gradient, 2nd iteration:
CRIT1=`$GREP "OS * x * (0| )\.00043" $log | wc -l`
CRIT2=`$GREP "OP * x * (\-0|\-)\.00043" $log | wc -l`
CRIT3=`$GREP "OP * y * (\-0|\-)\.0008[45]" $log | wc -l`
TEST[14]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[14]=3
ERROR[14]="SECOND GRADIENT NOT CORRECT"

# Hessian, 2nd iteration:

CRIT1=`$GREP "OS * x * (0| )\.40483[0-9]" $log | wc -l`
CRIT2=`$GREP "OP * x * (\-0|\-)\.40483[0-9] * (0| )\.40483[0-9]" $log | wc -l`
CRIT3=`$GREP "OP * y * (\-0|\-)\.22011[0-9] * (0| )\.22011[0-9] * (0| )\.84886[0-9]" $log | wc -l`
CRIT4=`$GREP "OS * y * (0| )\.37619[0-9]" $log | wc -l`
CRIT5=`$GREP "OP * x * (\-0|\-)\.23313[0-9] * (0| )\.1439[2-3][0-9]" $log | wc -l`
CRIT6=`$GREP "OP * y * (\-0|\-)\.37619[0-9] * (0| )\.23313[0-9] * (0| )\.37619[0-9]" $log | wc -l`
CRIT7=`$GREP "OS * z * (\-0|\-)\.00033[0-9]" $log | wc -l`
CRIT8=`$GREP "OP * z * (0| )\.00033[0-9] * (\-0|\-)\.00033[0-9]" $log | wc -l`
CRIT9=`$GREP "OP * z * (\-0|\-)\.00041[0-9]" $log | wc -l`
TEST[15]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
               $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9`
TEST[15]=10 # never mind, hjaaj March 2020
CTRL[15]=10
ERROR[15]="SECOND HESSIAN NOT CORRECT"

# Geometry optimization converged:
CRIT1=`$GREP "Norm of molecular gradient: * (0| )\.0000" $log | wc -l`
CRIT2=`$GREP "Change in energy since start of walk:(\-0| \-)\.00023305.* au" $log | wc -l`
CRIT3=`$GREP "Geometry optimization has converged\." $log | wc -l`
TEST[16]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[16]=3
ERROR[16]="GEOMETRY OPTIMIZATION FAILED"

# Dipole moment gradient:
CRIT1=`$GREP "OS * x * (0| )\.0389247" $log | wc -l`
CRIT2=`$GREP "OP * x * (\-0|\-)\.0389247" $log | wc -l`
CRIT3=`$GREP "OP * y * (0| )\.4111820" $log | wc -l`
CRIT4=`$GREP "OS * y * (0| )\.9477177" $log | wc -l`
CRIT5=`$GREP "OP * x * (\-0|\-)\.4850997" $log | wc -l`
CRIT6=`$GREP "OP * y * (\-0|\-)\.9477177" $log | wc -l`
CRIT7=`$GREP "OS * z * (0| )\.16428(5|6)(9|0)[0-9]" $log | wc -l`
CRIT8=`$GREP "OP * z * (\-0|\-)\.16428(5|6)(9|0)[0-9]" $log | wc -l`
TEST[17]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7  \+ $CRIT8`
CTRL[17]=9
ERROR[17]="DIPOLE MOMENT GRADIENT NOT CORRECT"

# Cioslowski charges:
CRIT1=`$GREP "OS * (0| )\.38364[0-9]" $log | wc -l`
CRIT2=`$GREP "OP *_1 * (\-0|\-)\.19182[0-9]" $log | wc -l`
CRIT3=`$GREP "OP *_2 * (\-0|\-)\.19182[0-9]" $log | wc -l`
TEST[18]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[18]=3
ERROR[18]="CIOSLOWSKI CHARGES NOT CORRECT"


# Static polarizabilities:
CRIT1=`$GREP "Ex * 12\.1243.. * 0*\.000000 * 0*\.000000" $log | wc -l`
CRIT2=`$GREP "Ey * 0*\.000000 * 28\.326... * 0*\.000000" $log | wc -l`
CRIT3=`$GREP "Ez * 0*\.000000 * 0*\.000000 * 10\.0759.." $log | wc -l`
TEST[19]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[19]=6
ERROR[19]="STATIC POLARIZABILITIES NOT CORRECT"

# Dynamic polarizabilities:
CRIT1=`$GREP "Ex * 12\.217.. * 0*\.000000 * 0*\.000000" $log | wc -l`
CRIT2=`$GREP "Ey * 0*\.000000 * 29\.35... * 0*\.000000" $log | wc -l`
CRIT3=`$GREP "Ez * 0*\.000000 * 0*\.000000 * 10\.130.." $log | wc -l`
CRIT4=`$GREP "Ex * 12\.377.. * 0*\.000000 * 0*\.000000" $log | wc -l`
CRIT5=`$GREP "Ey * 0*\.000000 * 31\.48... * 0*\.000000" $log | wc -l`
CRIT6=`$GREP "Ez * 0*\.000000 * 0*\.000000 * 10\.203.." $log | wc -l`
TEST[20]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6`
CTRL[20]=6
ERROR[20]="DYNAMIC POLARIZABILITIES NOT CORRECT"

# Rotational constants:
CRIT1=`$GREP "10375.\.[0-9]* * 13259\.[0-9]* * 11757\.[0-9]* MHz" $log | wc -l`
CRIT2=`$GREP "3\.460[0-9]* * (0| )\.442[0-9]* * (0| )\.3921[0-9]* cm\-1" $log | wc -l`
TEST[21]=`expr $CRIT1  \+ $CRIT2`
CTRL[21]=2
ERROR[21]="ROTATIONAL CONSTANTS NOT CORRECT"

# Vibrational frequencies:
CRIT1=`$GREP "1 * A1 * 1093\.9. * (0| )\.004984 * (0| )\.795 * (0| )\.0188" $log | wc -l`
CRIT2=`$GREP "3 * A1 * 708\.4. * (0| )\.003228 * 4\.496 * (0| )\.1064" $log | wc -l`
CRIT3=`$GREP "2 * B1 * 1021\.9. * (0| )\.004656 * 89\.00. * 2\.106(3|4)" $log | wc -l`
TEST[22]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[22]=3
ERROR[22]="VIBRATIONAL FREQUENCIES NOT CORRECT"

# IR intensities:
CRIT1=`$GREP "1 * A1 * ....... * ........ * (0| )\.795 * (0| )\.0188" $log | wc -l`
CRIT2=`$GREP "3 * A1 * ....... * ........ * 4\.496 * (0| )\.1064" $log | wc -l`
CRIT3=`$GREP "2 * B1 * ....... * ........ * 89\.00. * 2\.106(3|4)" $log | wc -l`
TEST[23]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[23]=3
ERROR[23]="IR INTENSITIES NOT CORRECT"

# Molecular partition function:
CRIT1=`$GREP "200\.00 * 4\.8150(D\+|E\+)06 * 3752\.7. * 1\.0072 * 1\.8199[0-9]*(D\+|E\+)10" $log | wc -l`
CRIT2=`$GREP "273\.15 * 1\.0496(D\+|E\+)07 * 5988\.1. * 1\.0325 * 6\.4895[0-9]*(D\+|E\+)10" $log | wc -l`
CRIT3=`$GREP "298\.15 * 1\.3065(D\+|E\+)07 * 6828\... * 1\.0467 * 9\.3379..(D\+|E\+)10" $log | wc -l`
CRIT4=`$GREP "1000\.00 * 2\.6916(D\+|E\+)08 * 41925\... * 2\.562. * 2\.891...(D\+|E\+)13" $log | wc -l`
CRIT5=`$GREP "2000\.00 * 1\.5226(D\+|E\+)09 * 11857.\... * 8\.83.. * 1\.5943..(D\+|E\+)15" $log | wc -l`
TEST[24]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5`
CTRL[24]=5
ERROR[24]="PARTITION FUNCTION NOT CORRECT"

# Total polarizabilities
CRIT1=`$GREP "EA * 4\.539... * (\-0|\-|0| )\.000000 * (\-0|\-|0| )\.000000" $log | wc -l`
CRIT2=`$GREP "EB * (\-0|\-|0| )\.000000 * 1\.8352.. * (\-0|\-|0| )\.000000" $log | wc -l`
CRIT3=`$GREP "EC * (\-0|\-|0| )\.000000 * (\-0|\-|0| )\.000000 * 1\.49310" $log | wc -l`
TEST[25]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[25]=4
ERROR[25]="TOTAL POLARIZABILITIES NOT CORRECT"

# Check for integrals:
CRIT1=`$GREP "TRACTL\: Integral transformation abandoned\," $log | wc -l`
CRIT2=`$GREP "the required MO integrals are already available\." $log | wc -l`
TEST[26]=`expr $CRIT1  \+ $CRIT2`
CTRL[26]=2
ERROR[26]="MO INTEGRALS NOT AVAILABLE"

# Linear response:
CRIT1=`$GREP "Value of linear response.*\: * 12.124" $log | wc -l`
CRIT2=`$GREP "Value of linear response.*\: * 12.217" $log | wc -l`
CRIT3=`$GREP "Value of linear response.*\: * 28.326" $log | wc -l`
CRIT4=`$GREP "Value of linear response.*\: * 29.35" $log | wc -l`
CRIT5=`$GREP "Value of linear response.*\: * 10.075" $log | wc -l`
CRIT6=`$GREP "Value of linear response.*\: * 10.130" $log | wc -l`
TEST[27]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ $CRIT6`
CTRL[27]=6
ERROR[27]="LINEAR RESPONSE NOT CORRECT"

PASSED=1
for i in 1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25 27
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
